package main

import (
	"fmt"
	"math"
	"math/cmplx"
)

// 全局变量必须用 var 关键字修饰
var aa = 3
var bb, cc = 4, 5

/*
变量默认值
*/
func variableZeroValue() {
	var a int
	var s string
	fmt.Println(a, s)
	fmt.Printf("a=%d, q=%q\n", a, s)
}

/*
变量初始化
*/
func variableInitialValue() {
	var a, b int = 3, 4
	var s string = "abc"
	fmt.Println(a, b, s)
}

/*
变量类型推断
*/
func variableTypeDeduction() {
	var a, b, c, d = 3, 4, true, "candywall"
	fmt.Println(a, b, c, d)
}

/*
定义变量的简单写法
*/
func variableShorterDefine() {
	a, b, c, d := 3, 4, true, "def"
	fmt.Println(a, b, c, d)
}

/*
验证欧拉公式
*/
func euler() {
	c := 3 + 4i
	fmt.Printf("euler formula, c = %f\n", cmplx.Abs(c))
	fmt.Printf("验证欧拉公式：%v\n", cmplx.Pow(math.E, 1i*math.Pi)+1)
	fmt.Printf("验证欧拉公式：%v\n", cmplx.Exp(1i*math.Pi)+1)
}

/*
已知直角三角形的两条直角边的长度用勾股定理求斜边的长度
*/
func triangle() {
	var a, b int = 3, 4
	var c int
	// 强制类型转换
	c = int(math.Sqrt(float64(a*a + b*b)))
	fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c)
}

/*
常量
*/
func consts() {
	const filename = "abc.txt"
	const a, b = 3, 4
	var c int
	c = int(math.Sqrt(a*a + b*b))
	fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c)
}

/*
模拟枚举
*/
func enums1() {
	const (
		cpp    = 0
		java   = 1
		python = 2
		golang = 3
	)
	println(cpp, java, python, golang)
}

/*
模拟枚举：简化写法
*/
func enums2() {
	const (
		cpp = iota
		_
		java
		python
		golang
	)
	println(cpp, java, python, golang)
}

func enums3() {
	const (
		b = 1 << (10 * iota)
		kb
		mb
		gb
		tb
		pb
	)
	println(b, kb, mb, gb, tb, pb)
}

func main() {
	variableZeroValue()
	variableInitialValue()
	variableTypeDeduction()
	variableShorterDefine()

	fmt.Println(aa, bb, cc)

	euler()

	triangle()

	enums1()
	enums2()
	enums3()
}
